﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Linq;
using System.Text;
using WinnerCMS.Common;
using WinnerCMS.Model;
using System.Data.Entity;

namespace WinnerCMS.Data
{
    /// <summary>
    /// 图片列表
    /// </summary>
    public class PhotoListDal
    {
        /// <summary>
        /// 添加图片列表
        /// </summary>
        /// <param name="model"></param>
        public void Add(PhotoList model)
        {
            using (Entities db = new Entities())
            {
                model.OrderId = GetMaxOrderId(db, model.ContentId, model.TempFlag) + 1;
                db.PhotoList.Add(model);
                db.SaveChangesAsync().Wait();
            }

        }
        /// <summary>
        /// 修改图片列表
        /// </summary>
        /// <param name="model"></param>
        public void Edit(PhotoList model)
        {
            using (Entities db = new Entities())
            {
                db.Entry(model).State = EntityState.Modified;
                db.SaveChangesAsync().Wait();
            }

        }
        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public PhotoList GetInfo(int id)
        {
            using (Entities db = new Entities())
            {
                return db.PhotoList.Find(id);
            }
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="contentId"></param>
        /// <param name="tempFlag"></param>
        /// <returns></returns>
        public IList<PhotoList> GetList(int contentId, string tempFlag)
        {
            using (Entities db = new Entities())
            {
                db.Configuration.ProxyCreationEnabled = false;

                var q = from x in db.PhotoList
                        select x;
                if (string.IsNullOrEmpty(tempFlag))
                {
                    q = q.Where(x => x.ContentId == contentId);
                }
                else
                {
                    q = q.Where(x => x.TempFlag == tempFlag);
                }
                q = q.OrderBy(x => x.OrderId);
                return q.ToList();
            }
        }
        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="Id"></param>
        public void Delete(int Id)
        {
            using (Entities db = new Entities())
            {
                PhotoList ad = db.PhotoList.FirstOrDefault(x => x.Id == Id);
                db.PhotoList.Remove(ad);
                db.SaveChangesAsync().Wait();
            }
        }

        /// <summary>
        /// 添加内容完成后，设置一下Id的归属
        /// </summary>
        /// <param name="contentId"></param>
        /// <param name="tempFlag"></param>
        public void FinishAction(int contentId, string tempFlag)
        {
            using (Entities db = new Entities())
            {
                var list = db.PhotoList.Where(x => x.TempFlag == tempFlag);
                foreach (var v in list)
                {
                    v.ContentId = contentId;
                    v.TempFlag = string.Empty;
                }
                db.SaveChangesAsync().Wait();
            }
        }
        /// <summary>
        /// 获取最大排序id
        /// </summary>
        /// <param name="db"></param>
        /// <param name="contentId"></param>
        /// <param name="tempFlag"></param>
        /// <returns></returns>
        private int GetMaxOrderId(Entities db, int contentId, string tempFlag)
        {
            var q = from x in db.PhotoList
                    select x;
            if (string.IsNullOrEmpty(tempFlag))
            {
                q = q.Where(x => x.ContentId == contentId);
            }
            else
            {
                q = q.Where(x => x.TempFlag == tempFlag);
            }
            return q.ToList().DefaultIfEmpty(new PhotoList() { OrderId = 0 }).Max(x => x.OrderId);
        }
    }
}